package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    List<User> users=new ArrayList<>();
    Connection connection=null;
    PreparedStatement pst=null;
    ResultSet resultSet=null;

    //jdbc进入数据库  查询
    public List<User> findAll() {
       //定义mybatis配置文件
        String resource = "mybatis-config.xml";
        try {
            //流操作
           InputStream inputStream =Resources.getResourceAsStream(resource);
           //会话工厂创建
      SqlSessionFactory sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
      //创建会话
            SqlSession sqlSession=sqlSessionFactory.openSession();
            //执行数据库操作
            List<User> users= sqlSession.getMapper(UserDao.class).findAll();
            return users;
        }catch(Exception e){
            throw new RuntimeException(e);
        }

        }

    }

